﻿using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Web;

namespace Wedn.Net.OAuth.Config
{
    /// <summary>
    /// QQ互联的配置数据
    /// </summary>
    [Serializable]
    public class QQConnectConfig
    {
        private readonly NameValueCollection _qzoneSection = ConfigurationManager.GetSection("oauthSectionGroup/qzoneSection") as NameValueCollection;

        /// <summary>
        /// 获取Authorization Code的URL地址
        /// </summary>
        /// <returns></returns>
        public string GetAuthorizeURL()
        {
            return _qzoneSection["AuthorizeUrl"];
        }
        /// <summary>
        /// 申请QQ登录成功后，分配给应用的appid
        /// </summary>
        /// <returns>string AppKey</returns>
        public string GetAppKey()
        {
            return _qzoneSection["AppKey"];
        }

        /// <summary>
        /// 申请QQ登录成功后，分配给网站的appkey
        /// </summary>
        /// <returns>string AppSecret</returns>
        public string GetAppSecret()
        {
            return _qzoneSection["AppSecret"];
        }

        /// <summary>
        /// 得到回调地址
        /// </summary>
        /// <returns></returns>
        public Uri GetCallBackUrl()
        {
            var callbackUrl = _qzoneSection["CallBackUrl"];
            if (Uri.IsWellFormedUriString(callbackUrl, UriKind.Absolute)) return new Uri(callbackUrl);
            var current = HttpContext.Current;
            if (current == null) return new Uri(callbackUrl);
            var currentUrl = current.Request.Url;
            callbackUrl = string.Format("{0}://{1}:{2}{3}",currentUrl.Scheme,currentUrl.Host, currentUrl.Port, callbackUrl);
            return new Uri(callbackUrl);
        }
    }
}
